#include "GameEntity.h"
#include "../../Scene/SceneNode.h"

GameEntity::GameEntity(int freq, std::string name) : _name(name), Updatable(freq) {
	//std::ostringstream oss (std::ostringstream::out);
	COLOR tempcolor = {1,1,1};
	this->setColor(tempcolor);
	this->_destroy = false;
}

GameEntity::~GameEntity(){
}


bool GameEntity::write(std::ostream& os) {
	if(this->getName() != "camera") {
		os << "*ENTITY*";
		os << this->getColor().r;
		os << ",";
		os << this->getColor().g;
		os << ",";
		os << this->getColor().b;
		os << "*NAME*";
		os << this->getName();
		os << "*FREQ*";
		os << this->maxFrequency;
		//os << this;
		os << "*ENTITYEOF*";
	}
	else {
		os <<"*ENTITYCAMERA*";
		os << this->getName();
		os << "*FREQ*";
		os << this->maxFrequency;
		os << "*ENTITYEOF*";
	}
	//os << this->getMySceneNode();
	return false;	//<-THIS NEEDS TO BE CHANGED, BUT I ADDED IT TO BE ABLE TO COMPILE!!!!!
}

void GameEntity::read(std::istream& is) {
	is >> this->_name;
	is >> this->maxFrequency;
	//handle pointer to this object
}

std::string GameEntity::getName() const
{
	return _name;
}


void* GameEntity::QueryInterface(InterfaceID i)
{
	//Check if the object Drawable
	if(i==DRAWABLE)
	{
		Drawable* pDraw = static_cast<Drawable*>(this);
		return (void*)(pDraw);
	}
	//Check if the object Updatable
	if(i==UPDATABLE)
	{
		Updatable* pUpdate = static_cast<Updatable*>(this);
		return (void*)(pUpdate);
	}
	//Check if the object Serializable
	if(i==SERIALIZABLE)
	{
		Serializable* pSeri = static_cast<Serializable*>(this);
		return (void*)(pSeri);
	}
	//If the object can not be converted return NULL to be checked by the calling function
	return NULL;
}

SceneNode* GameEntity::getMySceneNode() const
{
	return mySceneNode;
}

void GameEntity::setMySceneNode(SceneNode* sceneNode)
{
	mySceneNode = sceneNode;
}


POINT3D GameEntity::getPosition() const {
	return mySceneNode->getPosition();
}

void GameEntity::setPosition(POINT3D p) {
	mySceneNode->setPosition(p);
}

ORIENTATION GameEntity::getOrientation() const {
	return mySceneNode->getOrientation();
}

void GameEntity::setOrientation(ORIENTATION o) {
	mySceneNode->setOrientation(o);
}

void GameEntity::setColor(COLOR color) {
	this->_color.r = color.r;
	this->_color.g = color.g;
	this->_color.b = color.b;
}

COLOR GameEntity::getColor() {
	return _color;
}

void GameEntity::destroy() {
	_destroy = true;
}

bool GameEntity::toDestroy() {
	return _destroy;
}